import Vue from 'vue'
import VueRouter from 'vue-router'
//导入login组件
// import Login from '../components/common/Login.vue'
const Login=()=>import('../components/common/Login.vue')
//导入home组件
// import Home from '../views/Home.vue'
const Home=()=>import('../views/Home.vue')
//导入Welcome组件
// import Welcome from '../views/homechild/Welcome.vue'
const Welcome=()=>import('../views/homechild/Welcome.vue')
//导入Users组件
// import Users from '../views/homechild/Users.vue'
const Users=()=>import('../views/homechild/Users.vue')

//导入power的right
// import Rights from '../views/power/Rights.vue'
const Rights=()=>import('../views/power/Rights.vue')

//导入power的Roles
// import Roles from '../views/power/Roles.vue'
const Roles=()=>import('../views/power/Roles.vue')
const GoodsCate=()=>import('../views/goodscate/GoodsCate.vue')
const Params=()=>import('../views/params/Params.vue')
const GoodsList=()=>import('../views/goodslist/GoodsList.vue')
const AddGoods=()=>import('../views/goodslist/AddGoods.vue')
const Order=()=>import('../views/order/Order.vue')
const Report=()=>import('../views/report/Report.vue')

// import GoodsCate from '../views/goodscate/GoodsCate.vue'
// import Params from '../views/params/Params.vue'
// import GoodsList from '../views/goodslist/GoodsList.vue'
// import AddGoods from '../views/goodslist/AddGoods.vue'
// import Order from '../views/order/Order.vue'
// import Report from '../views/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  //设置默认的路由地址为登录页面
  {
    path:'/',
    redirect:'/login',
  },
  {
    path:'/login',
    component:Login,
  },
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:[
      {
        path:'/welcome',
        component:Welcome,
      },
      {
        path:'/users',
        component:Users,
      },
      {
        path:'/rights',
        component:Rights,
      },
      {
        path:'/roles',
        component:Roles,
      },
      {
        path:'/categories',
        component:GoodsCate,
      },
      {
        path:'/params',
        component:Params,
      },
      {
        path:'/goods',
        component:GoodsList,
      },
      {
        path:'/goods/add',
        component:AddGoods,
      },
      {
        path:'/orders',
        component:Order,
      },
      {
        path:'/reports',
        component:Report,
      }
    ]
  },
  

]

const router = new VueRouter({
  routes
})
//登录之后，如果删除token,及就是没有权限访问页面，需要使用
// 路由导航首位，将其直接定向到login
//为router挂在导航首位
router.beforeEach((to,from,next)=>{
  //to将要访问的路径 from代表从哪个路径开始访问 next表示放行，跳转到哪里
  if(to.path==="/login") return next();
  const tokenStr=window.sessionStorage.getItem("token");
  //token不存在或者为空，强制跳转到login
  if(!tokenStr){
    next('/login');
  }else{
    next();
  }
})

export default router
